<?php
defined('IN_MAIN') or exit('Invalid Access!');

function auto_placement($in_year, $in_month, $in_day){
	global $vars;
	$db=$vars['db'];

	$last_placement_time = mktime(23,59,59,$in_month,$in_day-1,$in_year);
	$last_placement_date = date('Y-m-d H:i:s', $last_placement_time);
	$t=@mysql_num_rows($r=mysql_query("select * from $db->users where status in ('normal','suspended') and matrix_upline='0' and ref!='0' order by cdate")) or 0;
	for($i=0;$i<$t;$i++){
		$member = mysql_fetch_assoc($r);
		$position_data = calc_next_matrix_upline_position($member['id']);
		$matrix_upline = $position_data['matrix_upline'];
		$matrix_position = $position_data['matrix_position'];
		
		if(!mysql_query($sql="update $db->users set matrix_upline='$matrix_upline', matrix_position='$matrix_position' where id='$member[id]' limit 1")){
			$placement_error.="UID: $member[id], Upline: $matrix_upline, Position: $matrix_position<br />\n$sql<br />";
		}
	}
	
	if($placement_error){
		$insert_errmsg.=($placement_error? "Failed Auto-Placement:<br />\n<br />\n$placement_error" : "");
		$result["errmsg"] .= $insert_errmsg;
	}else{
		$result["status"] = true;
	}
	
	return $result;
}
?>